package cn.gzjp.common.sms;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;


public class YouyiSms {
	private String url ="http://sms.ue35.net/sms/interface";
	private String userName="yhwy";
	private String password="537989";
	public YouyiSms(String userName,String password){
		this.userName=userName;
		this.password=password;
	}
	
	public static void main(String []args){
		YouyiSms sms = new YouyiSms("yhwy","537989");
		sms.sendSms("18602072825", "您正在进行微信关联会员操作，短信验证码为123456，请确保本人操作并保密!");
	}
	
	
	
	public String sendSms(String phone,String message){
		String sendUrl=this.url+"/sendmess.htm?";
		String result="";
		try{
			String postdata[]={
					"username="+URLEncoder.encode(this.userName,"UTF-8"),
					"userpwd="+URLEncoder.encode(this.password,"UTF-8"),
					"mobiles="+URLEncoder.encode(phone,"UTF-8"),
					"content="+URLEncoder.encode(message,"UTF-8"),
					"mobilecount=1"
			};
			result=http(sendUrl,implode("&",postdata));
		}catch(UnsupportedEncodingException e){
			e.printStackTrace();
		}
		return result;
	}
	
	
	public String sendSms(String[] phone,String message){
		String sendUrl=this.url+"/sendmess.htm";
		String result="";
		try{
			String postdata[]={
					"username="+URLEncoder.encode(this.userName,"UTF-8"),
					"userpwd="+URLEncoder.encode(this.password,"UTF-8"),
					"mobiles="+URLEncoder.encode(implode(";",phone),"UTF-8"),
					"content="+URLEncoder.encode(message,"UTF-8"),
					"mobilecount="+phone.length
			};
			result=http(sendUrl,implode("&",postdata));
		}catch(UnsupportedEncodingException e){
			e.printStackTrace();
		}
		return result;
	}

	public String getBalance(){
		String balanceUrl=this.url+"/getbalance.htm";
		String result="";
		try{
			String postdata[]={
					"username="+URLEncoder.encode(this.userName,"UTF-8"),
					"userpwd="+URLEncoder.encode(this.password,"UTF-8"),
			};
			result=http(balanceUrl,implode("&",postdata));
		}catch(UnsupportedEncodingException e){
			e.printStackTrace();
		}
		return result;
	}
	

	public String http(String url,String postdata){
		PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            URLConnection conn = realUrl.openConnection();
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent",  "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            out = new PrintWriter(conn.getOutputStream());
            out.print(postdata);
            out.flush();
            in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(in!=null){
                    in.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }
        return result;
	}
	
	public String implode(String fg,String[] phone){
		StringBuffer sb = new StringBuffer();
		sb. append(phone[0]);
		for(int i = 1; i < phone.length; i++){
		 sb. append(fg+phone[i]);
		}
		return sb.toString();
	}
}
